package com.xijia.wuyouwallet.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.utils.CharCheckUtil;
import com.xijia.wuyouwallet.utils.Constants;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/24 0024.
 */

public class FillBankcardActivity extends BaseActivity implements View.OnClickListener{

    private ImageView fillBankcard_back;
    private RelativeLayout fillBankcard_bank_ll,fillBankcard_bank_Provincell;
    private TextView fillBankcard_bank_tv,fillBankcard_bank_ProvinceTv;

    private LinearLayout fillBankcard_type_savingRl,fillBankcard_type_creditRl;
    private ImageView fillBankcard_type_savingImg,fillBankcard_type_creditImg;

    private EditText fillBankcard_phone_tv;

    private TextView fillBankcard_config;


    private LinearLayout fillBankcard_cvn2_ll,fillBankcard_indate_ll;
    private EditText fillBankcard_cvn2_et,fillBankcard_indate_et;


    private String cardType;
    private String bankName = null;
    private String bankNum,phone;

    private String bankProvinceName = null;
    private String bankProvinceCode = null;
    private String bankCityName = null;
    private String bankCityCode = null;

    private String cvn2 = null;
    private String indate = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fill_bankcard);
        initView();
    }

    private void initView(){
        fillBankcard_back = (ImageView) findViewById(R.id.fillBankcard_back);
        fillBankcard_back.setOnClickListener(this);
        fillBankcard_bank_ll = (RelativeLayout) findViewById(R.id.fillBankcard_bank_ll);
        fillBankcard_bank_ll.setOnClickListener(this);
        fillBankcard_bank_tv = (TextView) findViewById(R.id.fillBankcard_bank_tv);
        fillBankcard_bank_Provincell = (RelativeLayout) findViewById(R.id.fillBankcard_bank_Provincell);
        fillBankcard_bank_Provincell.setOnClickListener(this);
        fillBankcard_bank_ProvinceTv = (TextView) findViewById(R.id.fillBankcard_bank_ProvinceTv);

        fillBankcard_type_savingRl = (LinearLayout) findViewById(R.id.fillBankcard_type_savingRl);
        fillBankcard_type_savingRl.setOnClickListener(this);
        fillBankcard_type_creditRl = (LinearLayout) findViewById(R.id.fillBankcard_type_creditRl);
        fillBankcard_type_creditRl.setOnClickListener(this);
        fillBankcard_type_savingImg = (ImageView) findViewById(R.id.fillBankcard_type_savingImg);
        fillBankcard_type_creditImg = (ImageView) findViewById(R.id.fillBankcard_type_creditImg);

        fillBankcard_phone_tv = (EditText) findViewById(R.id.fillBankcard_phone_tv);
        fillBankcard_config = (TextView) findViewById(R.id.fillBankcard_config);
        fillBankcard_config.setOnClickListener(this);
        fillBankcard_cvn2_ll = (LinearLayout) findViewById(R.id.fillBankcard_cvn2_ll);
        fillBankcard_indate_ll = (LinearLayout) findViewById(R.id.fillBankcard_indate_ll);
        fillBankcard_cvn2_et = (EditText) findViewById(R.id.fillBankcard_cvn2_et);
        fillBankcard_indate_et = (EditText) findViewById(R.id.fillBankcard_indate_et);



        //asd
        bankNum = getIntent().getStringExtra("cardNum");
        //
        cardType = getIntent().getStringExtra("cardType");
        if (cardType.equals("0")){
            selectBankType(0);
        }else if (cardType.equals("1")){
            selectBankType(1);
        }
        //
        phone = MyApp.userData.getData().getPhone();
        fillBankcard_phone_tv.setText(phone);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fillBankcard_back:
                finish();
                break;
            case R.id.fillBankcard_bank_ll:
                jumpToPage(SelectBankActivity.class,true, Constants.REQUEST_DATA);
                break;
            case R.id.fillBankcard_type_savingRl:
                selectBankType(1);
                break;
            case R.id.fillBankcard_type_creditRl:
                selectBankType(0);
                break;
            case R.id.fillBankcard_config:
                if (cardType.equals("0")){
                    addBankCard(true);
                }else if (cardType.equals("1")){
                    if (bankCityCode == null){
                        showToast("请选择开户城市");
                        return;
                    }else {
                        addBankCard(false);
                    }
                }
                break;
            case R.id.fillBankcard_bank_Provincell://开户省份
                jumpToPage(SelectProvinceActivity.class,true, Constants.REQUEST_DATA);
                break;

        }
    }

    private void selectBankType(int type){
        if (type==1){
            fillBankcard_type_savingImg.setImageResource(R.mipmap.ic_addcard_type_selected);
            fillBankcard_type_creditImg.setImageResource(R.mipmap.ic_addcard_type_normal);
            cardType = "1";
            fillBankcard_bank_Provincell.setVisibility(View.VISIBLE);
            fillBankcard_cvn2_ll.setVisibility(View.GONE);
            fillBankcard_indate_ll.setVisibility(View.GONE);
        }else if (type==0){
            fillBankcard_type_savingImg.setImageResource(R.mipmap.ic_addcard_type_normal);
            fillBankcard_type_creditImg.setImageResource(R.mipmap.ic_addcard_type_selected);
            cardType = "0";
            fillBankcard_bank_Provincell.setVisibility(View.GONE);
            fillBankcard_cvn2_ll.setVisibility(View.VISIBLE);
            fillBankcard_indate_ll.setVisibility(View.VISIBLE);
        }
    }
    /**
     * 添加银行卡
     */
    private void addBankCard(boolean isCredit){
        if (bankName==null){
            showToast("请选择开户行名称");
            return;
        }
        final String phone = fillBankcard_phone_tv.getText().toString().trim();
        if (phone.isEmpty()){
            showToast("请输入手机号");
            return;
        }
        if (isCredit){
            cvn2 = fillBankcard_cvn2_et.getText().toString();
            if (cvn2.isEmpty()){
                showToast("请输入cvn码");
                return;
            }
            if (cvn2.length()!=3){
                showToast("cvn码有误");
                return;
            }
            indate = fillBankcard_indate_et.getText().toString();
            if (indate.isEmpty()){
                showToast("请输入信用卡有效期");
                return;
            }
            if (indate.length()!=4){
                showToast("有效期输入有误");
                return;
            }

        }
        if (!CharCheckUtil.isPhoneNum(phone)){
            showToast("手机号格式错误");
            return;
        }
        showProgressDialog(true);
        Map<String,Object> map = new HashMap<>();
        map.put("token", MyApp.userData.getData().getToken());
        map.put("user",MyApp.userData.getData().getUser_id());
        map.put("card_number",bankNum);
        map.put("card_type",cardType);
        map.put("card_tel",phone);
        map.put("card_bank",bankName);
        if (bankCityCode!=null){
            map.put("card_city_code",bankCityCode);
        }
        if (isCredit){
            map.put("card_cvn2",cvn2);
            map.put("card_validity_data",indate);
        }
        XUtil.Post(UrlConfig.BIND_BANKCARD_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                dismissProgressDialog();
                //MyLogger.wLog().e("绑定银行卡result="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    Bundle bundle = new Bundle();
                    bundle.putString("bankNum",bankNum);
                    bundle.putString("bankType",cardType);
                    bundle.putString("phone",phone);
                    jumpToPage(AddcardSuccessActivity.class,bundle,true);
                }else {
                    showToast(jsonObject.getString("msg"));
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                dismissProgressDialog();
                MyLogger.wLog().e("绑定银行卡onError="+ex.toString());
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==Constants.REQUEST_DATA&&resultCode==Constants.RESULT_DATA){
            bankName = data.getStringExtra("bankName");
            fillBankcard_bank_tv.setText(bankName);
        }else if (requestCode==Constants.REQUEST_DATA&&resultCode==Constants.RESULT_PROVINCE){
            bankProvinceName = data.getStringExtra("bankProvinceName");
            bankCityName = data.getStringExtra("bankCityName");
            bankProvinceCode = data.getStringExtra("bankProvinceCode");
            bankCityCode = data.getStringExtra("bankCityCode");
            fillBankcard_bank_ProvinceTv.setText(bankProvinceName+" "+bankCityName);
        }
    }
}
